import { Route, Routes } from 'react-router-dom';
import './App.css';
import AdminLayout from './layout';
import UserView from './views/User';
import RoleView from './views/Role';
import AddUser from './views/User/AddUser';
import Login from "./layout/Login";
import MainPage from "./views/MainPage/index";
import MenuView from "./views/Menu/index";
import CreateRole from "./views/Role/CreateRole/CreateRole";
import Commonuser_AccountManagement from "./layout/Commonuser_AccountManagement";
import Register from "./layout/Register";
import axios from 'axios';
import ProtectedRoute from "./component/ProtectedRoute";
import NotFoundPage from "./component/NotFoundPage";
import OperationLog from './views/OperationLog/index';
import LoginLogManagement from './views/loginlog/index';
import ForgotPassword from "./layout/ForgotPassword";
import useUserPermissions from "./component/permissionHook";

function App() {
    const { permissions, loading, error } = useUserPermissions();
    console.log('APP.jsx中的permissions:', permissions);
    const currentUser = localStorage.getItem('currentUser');
    let token = null;

    if (currentUser) {
        try {
            const userObject = JSON.parse(currentUser);
            token = userObject.token;
            console.log('token:', token);
        } catch (error) {
            console.error('未能解析用户的 token:', error);
        }
        if (token) {
            axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
        }
    }

    // 如果正在加载或发生错误，则显示相应消息
    if (loading) return <div>Loading...</div>;
    if (error) return <div>Error: {error.message}</div>;

    return (
        <div>
            <Routes>
                <Route path="/" element={<Login />} />
                <Route path="/register" element={<Register />} />
                <Route path="/forgot-password" element={<ForgotPassword />} />
                <Route path="/admin" element={<ProtectedRoute component={AdminLayout} requiredPermission="ROLE_admin" />}>
                    <Route index element={<ProtectedRoute component={MainPage} requiredPermission="PERMISSION_menu访问-首页" />} />
                    <Route path="mainpage" element={<ProtectedRoute component={MainPage} requiredPermission="PERMISSION_menu访问-首页" />} />
                    <Route path="user" element={<ProtectedRoute component={UserView} requiredPermission="PERMISSION_menu访问-用户管理" />} />
                    <Route path="user/add" element={<ProtectedRoute component={AddUser} requiredPermission="PERMISSION_menu访问-用户管理" />} />
                    <Route path="role" element={<ProtectedRoute component={RoleView} requiredPermission="PERMISSION_menu访问-角色管理" />} />
                    <Route path="role/create" element={<ProtectedRoute component={CreateRole} requiredPermission="PERMISSION_menu访问-角色管理" />} />
                    <Route path="menu" element={<ProtectedRoute component={MenuView} requiredPermission="PERMISSION_menu访问-菜单管理" />} />
                    <Route path="operationlog" element={<ProtectedRoute component={OperationLog} requiredPermission="PERMISSION_menu访问-操作日志" />} />
                    <Route path="loginlog" element={<ProtectedRoute component={LoginLogManagement} requiredPermission="PERMISSION_menu访问-登录日志" />} />
                    <Route path="user_profile" element={<ProtectedRoute component={Commonuser_AccountManagement} requiredPermission="ROLE_admin" />} />
                </Route>
                <Route path="/Commonuser_AccountManagement" element={<ProtectedRoute component={Commonuser_AccountManagement} requiredPermission="ROLE_admin" />} />
                <Route path="*" element={<NotFoundPage />} />
            </Routes>
        </div>
    );
}

export default App;